package cn.lyq.card.ui.main

import androidx.lifecycle.Observer
import cn.lyq.card.BuildConfig
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.aboutus.AboutUsVM
import cn.lyq.card.ui.aboutus.NewVersionFg
import cn.lyq.card.ui.achievement.AchievementFg
import cn.lyq.card.ui.course.CourseFg
import cn.lyq.card.ui.home.HomeFg
import cn.lyq.card.ui.my.MyFg
import cn.lyq.card.ui.rank.RankFg
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_main.*


class MainActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(AboutUsVM::class.java)
    }

    private val mTopDefault = arrayOf(
        R.drawable.ic_home_gray,
        R.drawable.ic_achievement_gray,
        R.drawable.ic_rank_list_unselect,
        R.drawable.ic_course_gray,
        R.drawable.ic_me_gray
    )

    private val mTopSelect = arrayOf(
        R.drawable.ic_home,
        R.drawable.ic_achievement,
        R.drawable.ic_rank_list_select,
        R.drawable.ic_course,
        R.drawable.ic_me
    )
    private val mClass = arrayOf(
        HomeFg::class.java,
        AchievementFg::class.java,
        RankFg::class.java,
        CourseFg::class.java,
        MyFg::class.java
    )
    private val mFgMap = mutableMapOf<Int, BaseFg>()
    private val mTab by lazy {
        arrayOf(tvHome, tvAchievement, tvRank, tvCourse, tvMy)
    }

    val spaceTime = 700
    var lastClickTime = 0L


    private var mCurrentIndex = -1

    override fun getLayoutId(): Int {
        return R.layout.act_main
    }

    override fun isLightBarColor(): Boolean {
        return mCurrentIndex == 1 || mCurrentIndex == 4
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_achievement)
    }

    override fun initListener() {
        super.initListener()

        mTab.withIndex().forEach {
            it.value.setCompoundDrawablesWithIntrinsicBounds(
                0, mTopDefault[it.index], 0, 0
            )
            it.value.setClick {
                clickTab(it.index)
            }
        }
    }

    override fun initObserver() {
        super.initObserver()
        clickTab(0)
        mModel.getApp().observe(this, Observer {
            if (it?.status == Status.SUCCESS) {
                if (BuildConfig.VERSION_NAME != it.requireData().version
                    && it.requireData().isForce()
                ) {
                    NewVersionFg.newInstance(it.requireData()).show(this)
                }
            }
        })
    }

    private fun clickTab(index: Int) {
        if (mCurrentIndex != index) {
            showFg(index)
            updateTab(index)
            mCurrentIndex = index
        }
    }

    private fun updateTab(index: Int) {
        if (mCurrentIndex != -1) {
            mTab[mCurrentIndex].isSelected = false
            mTab[mCurrentIndex].setCompoundDrawablesWithIntrinsicBounds(
                0, mTopDefault[mCurrentIndex], 0, 0
            )
        }
        mTab[index].isSelected = true
        mTab[index].setCompoundDrawablesWithIntrinsicBounds(
            0, mTopSelect[index], 0, 0
        )
    }

    private fun showFg(index: Int) {
        var fg = mFgMap[index]
        if (fg == null) {
            fg = mClass[index].newInstance()
            mFgMap[index] = fg
        }
        supportFragmentManager.beginTransaction().apply {
            mFgMap[mCurrentIndex]?.let {
                hide(it)
            }
            if (!fg.isAdded) {
                add(R.id.frameLayout, fg)
            }
            show(fg)
        }.commitAllowingStateLoss()
        if (index == 1 || index == 4) {
            setBarColor(true)
        } else {
            setBarColor()
        }
    }

    override fun onBackPressed() {
        val now = System.currentTimeMillis()
        if (now - lastClickTime < spaceTime) {
            moveTaskToBack(true)
        } else {
            ToastUtils.showShort(R.string.str_exit_app_tip)
            lastClickTime = now
        }
    }
}